package spring.mvc.practice;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Repository;
import org.hibernate.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;

@Repository("accountDao")
@Transactional
public class AccountDaoImpl extends HibernateDao implements AccountDao {

	/*
	@Transactional(readOnly = true)
	public List<AccountBean> findAll() {
		// TODO Auto-generated method stub
		
		return getSessionFactory().getCurrentSession()
		.createQuery("from Registration").list();
		
		
	}
*/
	// public List<Registration> findAllWithDetail() {
	// TODO Auto-generated method stub
	// return null;
	// }
	
	@Autowired
	private ConversionService conversionService; 
	
	

	public AccountBean findById(String id) {
		// TODO Auto-generated method stub
		return conversionService.convert((Registration) getSessionFactory().getCurrentSession().get(
				Registration.class, id),AccountBean.class);
	}

	public AccountBean findByEmail(String email) {

		
		return conversionService.convert((Registration) getSessionFactory().getCurrentSession()
				.createQuery("from Registration where upper(email)= upper(:email)")
				.setString("email", email).uniqueResult(),AccountBean.class);
	}
	
	public AccountBean save(AccountBean account) { 
		getSessionFactory().getCurrentSession().saveOrUpdate(createFromAccount(account));
		return account;		
	}
	
	public void changePassword(AccountBean account) {
		
		Registration registration =(Registration) getSessionFactory().getCurrentSession().get(
				Registration.class, account.getUsername());
		if (registration != null) {
			registration.setPassword(account.getPassword());
			getSessionFactory().getCurrentSession().update(registration);
		}
		
	}
	
	
	
	private Registration createFromAccount(AccountBean account)
	{
		Registration registration = new Registration();

		BeanUtils.copyProperties(account, registration);
		/*
	 	registration.setUsername(account.getUsername());
	 	registration.setEmail(account.getEmail());
	 	registration.setFirst(account.getFirst());
	 	registration.setMi(account.getMi());
	 	registration.setLast(account.getLast());
	 	registration.setPassword(account.getPassword());
	 	registration.setRole(account.getRole());
		registration.setPosition(account.getPosition());
		registration.setCompany(account.getCompany());
		registration.setAddress1(account.getAddress1());
		registration.setAddress2(account.getAddress2());
		registration.setCity(account.getCity());
		registration.setState(account.getState());
		registration.setZip(account.getZip());
		registration.setCountry(account.getCountry());
		registration.setReceiveMail(account.getReceiveMail());
		registration.setGender(account.getGender());
		registration.setDayPhone(account.getDayPhone());
		registration.setMobilePhone(account.getMobilePhone());
		registration.setFax(account.getFax());
		registration.setQuestion(account.getQuestion());
		registration.setAnswer(account.getAnswer());*/
	 return registration;
	}	
	
	
}
